//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import com.tools.acceleratecore.xlib.xlib.intf.IXObserver;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class XObserverAutoRemove<T> implements IXObserver<T> {
    protected List<WeakReference<T>> mListListener = new ArrayList();

    public XObserverAutoRemove() {
    }

    public void addListener(T listener) {
        List var2 = this.mListListener;
        synchronized(this.mListListener) {
            if (!this.isContains(listener)) {
                WeakReference<T> wr = new WeakReference(listener);
                this.mListListener.add(wr);
            }

        }
    }

    public void removeListener(T listener) {
    }

    public void removeAllListener() {
    }

    protected List<T> getListListener() {
        List var1 = this.mListListener;
        synchronized(this.mListListener) {
            this.checkListener();
            List<T> list = new ArrayList();
            Iterator var4 = this.mListListener.iterator();

            while(var4.hasNext()) {
                WeakReference<T> wr = (WeakReference)var4.next();
                T t = wr.get();
                if (t != null) {
                    list.add(t);
                }
            }

            return list;
        }
    }

    private boolean isContains(T listener) {
        List var2 = this.mListListener;
        synchronized(this.mListListener) {
            Iterator var4 = this.mListListener.iterator();

            Object t;
            do {
                if (!var4.hasNext()) {
                    return false;
                }

                WeakReference<T> wr = (WeakReference)var4.next();
                t = wr.get();
            } while(t == null || !t.equals(listener));

            return true;
        }
    }

    private void checkListener() {
        List var1 = this.mListListener;
        synchronized(this.mListListener) {
            int nIndex = 0;

            while(nIndex < this.mListListener.size()) {
                WeakReference<T> wr = (WeakReference)this.mListListener.get(nIndex);
                T t = wr.get();
                if (t == null) {
                    this.mListListener.remove(nIndex);
                } else {
                    ++nIndex;
                }
            }

        }
    }
}
